package com.yanqu.road.server.manager.activity.tonbing.task;


import com.yanqu.road.entity.enums.activity.tonbing.eTonBingTaskType;
import com.yanqu.road.server.manager.activity.tonbing.activity.CrossTonBingActivity;

import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

public abstract class BaseCrossTonBingActivityTask implements Runnable, Delayed {

    protected CrossTonBingActivity activity;
    protected long createTime;
    protected long beginTime;
    protected long finishTime;

    public BaseCrossTonBingActivityTask(CrossTonBingActivity activity, long beginTime) {
        this.activity = activity;
        this.beginTime = beginTime;
        this.createTime = System.currentTimeMillis();
    }

    // ======================== Runnable ============================

    @Override
    public void run() {
        runTask();
        this.finishTime = System.currentTimeMillis();
    }

    // ======================== Delayed ============================

    @Override
    public int compareTo(Delayed o) {
        BaseCrossTonBingActivityTask delayedTask = (BaseCrossTonBingActivityTask) o;
        return Long.compare(getBeginTime(), delayedTask.getBeginTime());
    }

    @Override
    public long getDelay(TimeUnit unit) {
        return unit.convert(getBeginTime() - System.currentTimeMillis(), TimeUnit.MILLISECONDS);
    }

    public long getBeginTime() {
        return beginTime;
    }

    public void setBeginTime(long beginTime) {
        this.beginTime = beginTime;
    }

    public abstract void runTask();

    public abstract String getId();

    public abstract eTonBingTaskType getType();

}
